//
//  DateViewController.m
//  Runone
//
//  Created by runone01 on 16/9/28.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "DateViewController.h"
#import "RODateHelper.h"

@interface DateViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong)NSArray *YearList;
@property(nonatomic,strong)NSArray *MonthList;
@property(nonatomic,strong)NSArray *DayList;
@property(nonatomic,strong)NSArray *HouseList;
@property(nonatomic,strong)NSArray *MinutsList;
@property(nonatomic,strong)NSArray *secondsList;

@property(nonatomic,copy)NSString *yearStr;
@property(nonatomic,copy)NSString *monthStr;
@property(nonatomic,copy)NSString *dayStr;
@property(nonatomic,copy)NSString *houseStr;
@property(nonatomic,copy)NSString *minStr;
@property(nonatomic,copy)NSString *secondStr;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *timeLab;

@property (assign, nonatomic) NSInteger selectedIndex;

@property (assign, nonatomic) NSInteger currentYear;
@property (assign, nonatomic) NSInteger currentMonth;
@property (assign, nonatomic) NSInteger days; //当前月份的天数

@end

@implementation DateViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDate *date=[RODateHelper  localeDate];
//    NSDateFormatter *format=[[NSDateFormatter alloc] init];
//    [format setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
//    NSString *CurrentTime = [format stringFromDate:date];
    NSString *CurrentTime = [RODateHelper dateToString:date withFormat:nil];
    _timeLab.text = CurrentTime;
    
     NSString *tempYear = [CurrentTime substringWithRange:NSMakeRange(0, 4)];
    _currentYear        = [tempYear integerValue];
    _yearStr            = [tempYear stringByAppendingString:@"年"];
    
    NSString *tempMonStr = [CurrentTime substringWithRange:NSMakeRange(5,2)];
    _currentMonth        = [tempMonStr integerValue];
    _monthStr            = [NSString stringWithFormat:@"%@月",[CurrentTime substringWithRange:NSMakeRange(5,2)]];
     NSLog(@"%@",_monthStr);
    [self judgeYesrAndMonth];
    
    
    _dayStr=[NSString stringWithFormat:@"%@日",[CurrentTime substringWithRange:NSMakeRange(8,2)]];
    NSLog(@"%@",_dayStr);
    _houseStr=[NSString stringWithFormat:@"%@时",[CurrentTime substringWithRange:NSMakeRange(11,2)]];
     NSLog(@"%@",_houseStr);
    _minStr=[NSString stringWithFormat:@"%@分",[CurrentTime substringWithRange:NSMakeRange(14,2)]];
     NSLog(@"%@",_minStr);
    _secondStr=[NSString stringWithFormat:@"%@秒",[CurrentTime substringWithRange:NSMakeRange(17,2)]];
     NSLog(@"%@",_secondStr);

    
    CGFloat myWidth  = [UIScreen mainScreen].bounds.size.width ;
    
    self.preferredContentSize=CGSizeMake(myWidth - 10, 256);
    _pickerView.showsSelectionIndicator=YES;
    _pickerView.dataSource = self;
    _pickerView.delegate = self;
    
    [self configDate];
    
   // _pickerView.backgroundColor=[UIColor greenColor];
    _YearList = [[NSArray alloc]initWithObjects:@"2010年",@"2011年",@"2012年",@"2013年",@"2014年",@"2015年",@"2016年",@"2017年",@"2018年",@"2019年",nil];
    
    _MonthList = @[@"01月",@"02月",@"03月",@"04月",@"05月",@"06月",@"07月",@"08月",@"09月",@"10月",@"11月",@"12月"];
    
    [self judgeDays];
    
    _HouseList = [[NSArray alloc]initWithObjects:@"01时",@"02时",@"03时",@"04时",@"05时",@"06时",@"07时",@"08时",@"09时",@"10时",@"11时",@"12时",@"13时",@"14时",@"15时",@"16时",@"17时",@"18时",@"19时",@"20时",@"21时",@"22时",@"23时",@"24时",nil];
     _MinutsList = [[NSArray alloc]initWithObjects:@"01分",@"02分",@"03分",@"04分",@"05分",@"06分",@"07分",@"08分",@"09分",@"10分",@"11分",@"12分",@"13分",@"14分",@"15分",@"16分",@"17分",@"18分",@"19分",@"20分",@"21分",@"22分",@"23分",@"24分",@"25分",@"26分",@"27分",@"28分",@"29分",@"30分",@"31分",@"32分",@"33分",@"34分",@"35分",@"36分",@"37分",@"38分",@"39分",@"40分",@"41分",@"42分",@"43分",@"44分",@"45分",@"46分",@"47分",@"48分",@"49分",@"50分",@"51分",@"52分",@"53分",@"54分",@"55分",@"56分",@"57分",@"58分",@"59分",nil];
    _secondsList = [[NSArray alloc]initWithObjects:@"01秒",@"02秒",@"03秒",@"04秒",@"05秒",@"06秒",@"07秒",@"08秒",@"09秒",@"10秒",@"11秒",@"12秒",@"13秒",@"14秒",@"15秒",@"16秒",@"17秒",@"18秒",@"19秒",@"20秒",@"21秒",@"22秒",@"23秒",@"24秒",@"25秒",@"26秒",@"27秒",@"28秒",@"29秒",@"30秒",@"31秒",@"32秒",@"33秒",@"34秒",@"35秒",@"36秒",@"37秒",@"38",@"39秒",@"40秒",@"41秒",@"42秒",@"43秒",@"44秒",@"45秒",@"46秒",@"47秒",@"48秒",@"49秒",@"50",@"51秒",@"52秒",@"53秒",@"54秒",@"55秒",@"56秒",@"57秒",@"58秒",@"59秒",nil];
    
    if ([_YearList containsObject:_yearStr]) {
        NSInteger i =[_YearList indexOfObject:_yearStr];
        [_pickerView selectRow:i inComponent:0 animated:YES];
    }
    else
    {
        return;
    }
    if ([_MonthList containsObject:_monthStr]) {
        NSInteger i =[_MonthList indexOfObject:_monthStr];
        [_pickerView selectRow:i inComponent:1 animated:YES];
    }
    else
    {
        return;
    }
    if ([_DayList containsObject:_dayStr]) {
        NSInteger i =[_DayList indexOfObject:_dayStr];
        [_pickerView selectRow:i inComponent:2 animated:YES];
    }
    else
    {
        return;
    }
    if ([_HouseList containsObject:_houseStr]) {
        NSInteger i =[_HouseList indexOfObject:_houseStr];
        [_pickerView selectRow:i inComponent:3 animated:YES];
    }
    else
    {
        return;
    }
    if ([_MinutsList containsObject:_minStr]) {
        NSInteger i =[_MinutsList indexOfObject:_minStr];
        [_pickerView selectRow:i inComponent:4 animated:YES];
    }
    else
    {
        return;
    }
    if ([_secondsList containsObject:_secondStr]) {
        NSInteger i =[_secondsList indexOfObject:_secondStr];
        [_pickerView selectRow:i inComponent:5 animated:YES];
    }
    else
    {
        return;
    }
    // Do any additional setup after loading the view from its nib.
}

- (void) configDate {
    NSDate *date = [RODateHelper localeDate];
//    NSDateFormatter *format=[[NSDateFormatter alloc] init];
//    [format setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *CurrentTime = [RODateHelper dateToString:date withFormat:nil];
    
    _timeLab.text = CurrentTime;
    NSString *tempYear = [CurrentTime substringWithRange:NSMakeRange(0, 4)];
    
    _yearStr   = [tempYear stringByAppendingString:@"年"];
    
    [self judgeYesrAndMonth];
    
    _monthStr  = [NSString stringWithFormat:@"%@月",[CurrentTime substringWithRange:NSMakeRange(5,2)]];
    NSLog(@"%@",_monthStr);
    _dayStr    = [NSString stringWithFormat:@"%@日",[CurrentTime substringWithRange:NSMakeRange(8,2)]];
    NSLog(@"%@",_dayStr);
    _houseStr  = [NSString stringWithFormat:@"%@时",[CurrentTime substringWithRange:NSMakeRange(11,2)]];
    NSLog(@"%@",_houseStr);
    _minStr    = [NSString stringWithFormat:@"%@分",[CurrentTime substringWithRange:NSMakeRange(14,2)]];
    NSLog(@"%@",_minStr);
    _secondStr = [NSString stringWithFormat:@"%@秒",[CurrentTime substringWithRange:NSMakeRange(17,2)]];
    NSLog(@"%@",_secondStr);
}

- (void) judgeYesrAndMonth {
    
    if (_currentMonth == 1 || _currentMonth == 3 || _currentMonth == 5 ||
        _currentMonth == 7 || _currentMonth == 8 || _currentMonth == 10 ||
        _currentMonth == 12) {
        _days = 31;
    }
    
    if (_currentMonth == 4 || _currentMonth == 6 || _currentMonth == 9 ||
        _currentMonth == 11 ) {
        _days = 30;
    }

    if (_currentMonth == 2) {
        if (_currentYear % 400 == 0 || (_currentYear % 4 == 0 && _currentYear % 100 != 0)) { //闰年 29天
            _days = 29;
        }else {
            _days = 28;
        }
    }
}

- (void) judgeDays {
    if (_days == 31) {
        _DayList= [[NSArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",@"29日",@"30日",@"31日",nil];
    }
    
    if (_days == 30) {
        _DayList= [[NSArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",@"29日",@"30日",nil];
    }
    
    if (_days == 29) {
        _DayList= [[NSArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",@"29日",nil];
    }
    
    if (_days == 28) {
        _DayList= [[NSArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",nil];
    }
}


- (IBAction)cancle:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)confirm:(UIButton *)sender {
    if (_dateBlock) {
        if ([_monthStr containsString:@"月"]) {
            _monthStr = [_monthStr stringByReplacingOccurrencesOfString:@"月" withString:@""];
        }
        if ([_dayStr containsString:@"日"]) {
            _dayStr = [_dayStr stringByReplacingOccurrencesOfString:@"日" withString:@""];
        }
        if ([_houseStr containsString:@"时"]) {
            _houseStr = [_houseStr stringByReplacingOccurrencesOfString:@"时" withString:@""];
        }
        if ([_minStr containsString:@"分"]) {
            _minStr = [_minStr stringByReplacingOccurrencesOfString:@"分" withString:@""];
        }
        if ([_secondStr containsString:@"秒"]) {
            _secondStr = [_secondStr stringByReplacingOccurrencesOfString:@"秒" withString:@""];
        }
        _dateBlock(_timeLab.text);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 6;
}
// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return [_YearList count];
    }
    if (component == 1 ) {
        return _MonthList.count;
    }
    if (component == 2 ) {
        return _DayList.count;
    }
    if (component == 3 ) {
        return _HouseList.count;
    }
    if (component == 4 ) {
        return _MinutsList.count;
    }
    else
    {
        return _secondsList.count;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component==0) {
        return 60;
    }
    else
    {
        return 40;
    }
    
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _selectedIndex = row;
    if (component == 0) {
        NSString *tempStr = [_YearList objectAtIndex:row];
        _yearStr = [tempStr substringWithRange:NSMakeRange(0,4)];
        NSLog(@"yearStr=%@",_yearStr);
    }
    if (component == 1) {
        
        [self judgeYesrAndMonth];
        [self judgeDays];
        [_pickerView reloadComponent:2];
        NSString * str    = [_MonthList objectAtIndex:row];
        _monthStr         = [str substringWithRange:NSMakeRange(0,2)];
        NSLog(@"_monthStr=%@",_monthStr);
    }
    if (component == 2) {
        
        NSString * str  = [_DayList objectAtIndex:row];
        _dayStr         = [str substringWithRange:NSMakeRange(0,2)];
//        _dayStr= [str stringByReplacingOccurrencesOfString:@"日" withString:@""];
        NSLog(@"dayStr=%@",_dayStr);
    }
    if (component == 3) {
        
        NSString * str = [_HouseList objectAtIndex:row];
        _houseStr =[str substringWithRange:NSMakeRange(0,2)];
        NSLog(@"hourStr=%@",_houseStr);
    }
    if (component == 4) {
        NSString * str = [_MinutsList objectAtIndex:row];
        _minStr = [str substringWithRange:NSMakeRange(0,2)];;
        NSLog(@"minStr=%@",_minStr);
    }
    
    if (component == 4) {
        NSString * str = [_secondsList objectAtIndex:row];
        _secondStr = [str substringWithRange:NSMakeRange(0,2)];;
        NSLog(@"secStr=%@",_secondStr);
    }
    
    NSString *yStr   = [_yearStr substringWithRange:NSMakeRange(0, 4)];
    NSString *mStr   = [_monthStr substringWithRange:NSMakeRange(0, 2)];
    NSString *dStr   = [_dayStr substringWithRange:NSMakeRange(0, 2)];
    NSString *hStr   = [_houseStr substringWithRange:NSMakeRange(0, 2)];
    NSString *minStr = [_minStr substringWithRange:NSMakeRange(0, 2)];
    NSString *secStr = [_secondStr substringWithRange:NSMakeRange(0, 2)];
    
        _timeLab.text = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",yStr,mStr,dStr,hStr,minStr,secStr];
    NSLog(@"%@",_timeLab.text);
    
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        return [_YearList objectAtIndex:row];
    }
    if (component == 1) {
        return [_MonthList objectAtIndex:row];
    }
    if (component == 2) {
        return [_DayList objectAtIndex:row];
    }
    if (component == 3) {
        return [_HouseList objectAtIndex:row];
    }
    if (component == 4) {
        return [_MinutsList objectAtIndex:row];
    }
    else {
        return [_secondsList objectAtIndex:row];
    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
          forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc] init];
        
        pickerLabel.font = [UIFont systemFontOfSize:17];
        
//        pickerLabel.adjustsFontSizeToFitWidth =YES;
        
        if (component == 0) {
            pickerLabel.text = [_YearList objectAtIndex:row];
        }
        if (component == 1) {
            pickerLabel.text = [_MonthList objectAtIndex:row];
        }
        if (component == 2) {
            pickerLabel.text = [_DayList objectAtIndex:row];
        }
        if (component == 3) {
            pickerLabel.text = [_HouseList objectAtIndex:row];
        }
        if (component == 4) {
            pickerLabel.text = [_MinutsList objectAtIndex:row];
        }
        if (component == 5) {
            pickerLabel.text = [_secondsList objectAtIndex:row];
        }
    }
    return pickerLabel;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
